import 'package:flutter/material.dart';
import 'package:spring_dict/UI/Pages/AppsPage/AppsPage.dart';
import 'package:spring_dict/UI/Pages/SearchPage/SearchPage.dart';
import 'package:spring_dict/UI/Pages/UserCenterPage.dart';
import 'package:spring_dict/global.dart';

class MainPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Directionality(
        textDirection: TextDirection.rtl, child: MainPageStateful());
  }
}

class MainPageStateful extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => MainPageState();
}

class MainPageState extends State<MainPageStateful> {
  int currentPage = 0;
  var pageViewController = PageController();

  goToPage(int index, {bool ignore = false}) {
    setState(() {
      currentPage = index;
      if (!ignore) pageViewController.animateToPage(currentPage, duration: Duration(milliseconds: 300), curve: Curves.easeOut);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        controller: pageViewController,
        onPageChanged: (index) => goToPage(index, ignore: true),
        children: <Widget>[
          SearchPage(),
          AppsPage(),
          UserCenterPage(),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: currentPage,
        items: [
          BottomNavigationBarItem(
            title: Text("باش بەت"),
            icon: Icon(Icons.search),
          ),
          BottomNavigationBarItem(
              title: Text("ئىقتىدارلار"), icon: Icon(Icons.apps)),
          BottomNavigationBarItem(
              title: Text("ئەزا مەركىزى"), icon: Icon(Icons.person)),
        ],
        onTap: goToPage,
      ),
    );
  }
}
